home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_access.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  4.8 KB  |  184 lines

  1. /*
  2.  * http_access: Security options etc.
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8.  
  9. #include "httpd.h"
  10.  
  11. int in_domain(char *domain, char *what) {
  12.     int dl=strlen(domain);
  13.     int wl=strlen(what);
  14.  
  15.     if((wl-dl) >= 0)
  16.         return(!strcmp(domain,&what[wl-dl]));
  17.     else
  18.         return 0;
  19. }
  20.  
  21. int in_ip(char *domain, char *what) {
  22.     return(!strncmp(domain,what,strlen(domain)));
  23. }
  24.  
  25. int find_allow(int x, int m) {
  26.     register int y;
  27.  
  28.     if(sec[x].num_allow[m] < 0)
  29.         return 1;
  30.  
  31.     for(y=0;y<sec[x].num_allow[m];y++) {
  32.         if(!strcmp("all",sec[x].allow[m][y])) 
  33.             return 1;
  34.         if(remote_host && isalpha(remote_host[0]))
  35.             if(in_domain(sec[x].allow[m][y],remote_host))
  36.                 return 1;
  37.         if(in_ip(sec[x].allow[m][y],remote_ip))
  38.             return 1;
  39.     }
  40.     return 0;
  41. }
  42.  
  43. int find_deny(int x, int m) {
  44.     register int y;
  45.  
  46.     if(sec[x].num_deny[m] < 0)
  47.         return 1;
  48.  
  49.     for(y=0;y<sec[x].num_deny[m];y++) {
  50.         if(!strcmp("all",sec[x].deny[m][y])) 
  51.             return 1;
  52.         if(remote_host && isalpha(remote_host[0]))
  53.             if(in_domain(sec[x].deny[m][y],remote_host))
  54.                 return 1;
  55.         if(in_ip(sec[x].deny[m][y],remote_ip))
  56.             return 1;
  57.     }
  58.     return 0;
  59. }
  60.  
  61. void check_dir_access(int x, int m, int *w, int *n) {
  62.     if(sec[x].auth_type)
  63.         auth_type = sec[x].auth_type;
  64.     if(sec[x].auth_name)
  65.         auth_name = sec[x].auth_name;
  66.     if(sec[x].auth_pwfile)
  67.         auth_pwfile = sec[x].auth_pwfile;
  68.     if(sec[x].auth_grpfile)
  69.         auth_grpfile = sec[x].auth_grpfile;
  70.  
  71.     if(sec[x].order[m] == ALLOW_THEN_DENY) {
  72.         if(find_allow(x,m))
  73.             *w=1;
  74.         if(find_deny(x,m))
  75.             *w=0;
  76.         if(sec[x].num_auth[m])
  77.             *n=x;
  78.     } else {
  79.         if(find_deny(x,m))
  80.             *w=0;
  81.         if(find_allow(x,m))
  82.             *w=1;
  83.         if(sec[x].num_auth[m])
  84.             *n=x;
  85.     }
  86. }
  87.  
  88. void evaluate_access(char *p, struct stat *finfo, int m, int *allow, 
  89.                      char *allow_options) 
  90. {
  91.     int will_allow, need_auth, num_dirs;
  92.     char opts[MAX_STRING_LEN], override[MAX_STRING_LEN];
  93.     char path[MAX_STRING_LEN], d[MAX_STRING_LEN];
  94.     register int x,y,z,n;
  95.  
  96.     if(S_ISDIR(finfo->st_mode)) strcpy_dir(path,p);
  97.     else strcpy(path,p);
  98.  
  99.     num_dirs = count_dirs(path);
  100.     will_allow=1;need_auth=-1;
  101.     auth_type=NULL;auth_name=NULL;auth_pwfile=NULL;auth_grpfile=NULL;
  102.     user[0] = '\0';
  103.     for(x=0;x<num_dirs;x++) {
  104.         opts[x] = OPT_ALL;
  105.         override[x] = OR_ALL;
  106.     }
  107.  
  108.     for(x=0;x<num_sec;x++) {
  109.         if(!strncmp(path,sec[x].d,strlen(sec[x].d))) {
  110.             for(y=count_dirs(sec[x].d) - 1;y<num_dirs;y++) {
  111.                 opts[y] = sec[x].opts;
  112.                 override[y] = sec[x].override;
  113.             }
  114.             check_dir_access(x,m,&will_allow,&need_auth);
  115.         }
  116.     }
  117.     n=num_dirs-1;
  118.     if((override[n]) || (!(opts[n] & OPT_SYM_LINKS))) {
  119.         for(x=0;x<num_dirs;x++) {
  120.             y = num_sec;
  121.             make_dirstr(path,x+1,d);
  122.             if(!(opts[x] & OPT_SYM_LINKS)) {
  123.                 struct stat fi;
  124.                 lstat(d,&fi);
  125.                 if(!(S_ISDIR(fi.st_mode))) {
  126.                     char errstr[MAX_STRING_LEN];
  127.                     sprintf(errstr,"httpd: will not follow link %s",d);
  128.                     log_error(errstr);
  129.                     *allow=0;
  130.                     *allow_options = OPT_NONE;
  131.                     return;
  132.                 }
  133.             }
  134.             if(override[x]) {
  135.                 parse_htaccess(d,override[x]);
  136.                 if(num_sec != y) {
  137.                     for(z=count_dirs(sec[y].d) - 1;z<num_dirs;z++) {
  138.                         opts[z] = sec[y].opts;
  139.                         override[z] = sec[y].override;
  140.                     }
  141.                     check_dir_access(y,m,&will_allow,&need_auth);
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     if((!(S_ISDIR(finfo->st_mode))) && (!(opts[n] & OPT_SYM_LINKS))) {
  147.         struct stat fi;
  148.         lstat(path,&fi);
  149.         if(!(S_ISREG(fi.st_mode))) {
  150.             char errstr[MAX_STRING_LEN];
  151.             sprintf(errstr,"httpd: will not follow link %s",path);
  152.             log_error(errstr);
  153.             *allow=0;
  154.             *allow_options = OPT_NONE;
  155.             return;
  156.         }
  157.     }
  158.     *allow = will_allow;
  159.     if(will_allow) {
  160.         *allow_options = opts[num_dirs-1];
  161.         if(need_auth >= 0)
  162.             check_auth(&sec[need_auth],m);
  163.     }
  164.     else *allow_options = 0;
  165. }
  166.  
  167. void kill_security() {
  168.     register int x,y,m;
  169.  
  170.     for(x=0;x<num_sec;x++) {
  171.         free(sec[x].d);
  172.         for(m=0;m<METHODS;m++) {
  173.             for(y=0;y<sec[x].num_allow[m];y++)
  174.                 free(sec[x].allow[m][y]);
  175.             for(y=0;y<sec[x].num_deny[m];y++)
  176.                 free(sec[x].deny[m][y]);
  177.             for(y=0;y<sec[x].num_auth[m];y++)
  178.                 free(sec[x].auth[m][y]);
  179.         }
  180.     }
  181. }
  182.  
  183.  
  184.